package com.shuzi.MediaShare.gui.search;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.gui.movie.MovieDetailPlayerActivity;
import com.shuzi.MediaShare.gui.movie.MovieHomeAdapter;
import com.shuzi.MediaShare.model.MovieInfo;
import com.shuzi.MediaShare.net.Protocol;
import com.shuzi.MediaShare.view.LoadMoreScrollListener;
import com.shuzi.MediaShare.view.LoadMoreScrollListener.setOnScrollToEndListener;

/**
 * 搜索结果页面
 * 
 * @author lung 下午1:09:06 2013
 */
public class SearchResultActivity extends SearchBaseFrameAtivity
{

	private String key = "";

	/** 每次获取数据最少值 */
	private final String LIMIT = "15";

	/** 第一次获取数据 */
	private final int LOAD_DATA_FIRST = 0;

	/** 加载更多数据 */
	private final int LOAD_MORE_DATA = 1;

	/** 显示列表 */
	private GridView gridView = null;

	/** 电影列表 */
	private List<MovieInfo> mediaInfos = null;

	/** 每次加载的数据 */
	private List<MovieInfo> loadData = null;

	/** 是否在加载数据 */
	private boolean loadingData = false;

	/** 是否加载完数据 */
	private boolean loadDataEnd = false;

	/** 适配器 */
	private MovieHomeAdapter adapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		key = getIntent().getStringExtra("KEY");
		mediaInfos = new ArrayList<MovieInfo>();
		adapter = new MovieHomeAdapter(this, mediaInfos);
		searchEditText.setText(key);
		searchButton.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				key = searchEditText.getText().toString();
				loadData(LOAD_DATA_FIRST, 0);
				// Toast.makeText(SearchResultActivity.this, key,
				// Toast.LENGTH_SHORT).show();

			}
		});
		gridView = (GridView) findViewById(R.id.movie_gridView);
		gridView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id)
			{
				if (mediaInfos != null && !mediaInfos.isEmpty())
				{
					Intent intent = new Intent();
					intent.setClass(SearchResultActivity.this,
							MovieDetailPlayerActivity.class);
					intent.putExtra("MOVIEINFO", mediaInfos.get(position));
					startActivity(intent);
				}
			}
		});
		gridView.setOnScrollListener(new LoadMoreScrollListener(ImageLoader
				.getInstance(), true, true, new setOnScrollToEndListener()
		{

			@Override
			public void loadMoreWhenScrollToEnd()
			{
				loadMoreData();

			}

		}));
		loadData(LOAD_DATA_FIRST, 0);
	}

	/**
	 * 加载更多数据
	 */
	private void loadMoreData()
	{
		if (mediaInfos != null && !loadingData && !loadDataEnd)
		{
			loadData(LOAD_MORE_DATA, mediaInfos.size());
			loadingData = true;
		}
	}

	@Override
	protected boolean initData(Integer... params)
	{
		int type = params[0];
		boolean isSuccess = true;
		switch (type)
		{
		case LOAD_DATA_FIRST:
			loadData = Protocol.queryDes3(params[1] + "", LIMIT, key);
			// loadData = Protocol.queryDes2(typeId, params[1] + "", LIMIT,
			// selectAreaItem, selectTypeItem, selectTimeItem);
			if (loadData == null)
			{
				isSuccess = false;
			} else if (loadData.isEmpty())
			{
				isSuccess = false;
			}
			break;
		case LOAD_MORE_DATA:
			if (loadData != null)
			{
				loadData.clear();
				loadData = null;
			}
			// loadData = Protocol.queryDes2(typeId, params[1] + "", LIMIT,
			// selectAreaItem, selectTypeItem, selectTimeItem);
			loadData = Protocol.queryDes3(params[1] + "", LIMIT, key);
			if (loadData == null)
			{
				isSuccess = false;
			} else if (loadData.isEmpty())
			{
				isSuccess = true;
			}
			break;

		}
		return isSuccess;
	}

	@Override
	protected void refreshView(boolean isSuccessful, Integer... params)
	{
		switch (params[0])
		{
		case LOAD_DATA_FIRST:
			if (isSuccessful)
			{
				mediaInfos.clear();
				mediaInfos.addAll(loadData);
				gridView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				if (loadData.size() < Integer.parseInt(LIMIT))
				{
					loadDataEnd = true;
				}
			} else
			{
				// errorView.setVisibility(View.VISIBLE);
				Toast.makeText(SearchResultActivity.this, "暂无搜索信息",
						Toast.LENGTH_SHORT).show();
			}
			break;
		case LOAD_MORE_DATA:
			if (isSuccessful)
			{
				mediaInfos.addAll(loadData);
				adapter.notifyDataSetChanged();

				if (loadData.size() < Integer.parseInt(LIMIT))
				{
					loadDataEnd = true;
				}
			} else
			{

				Toast.makeText(SearchResultActivity.this, "加载更多数据发生错误",
						Toast.LENGTH_SHORT).show();
			}
			loadingData = false;
			break;
		}
	}

	@Override
	protected void tryAgain()
	{
		loadData(LOAD_DATA_FIRST, 0);

	}

	@Override
	protected View setCenterView()
	{
		View view = View.inflate(this, R.layout.search_result, null);
		return view;
	}

}
